/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.car;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.attach.AttachLink;
import com.unis.model.car.Car;
import com.unis.service.attach.AttachmentService;
import com.unis.service.car.CarService;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2013-01-11
 */
@Controller
@RequestMapping("/car")
public class CarController {

    private CarService carService;
    private AttachmentService attachmentService;

    /****************************************
     * Insert a instace of Car
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertCar(@RequestBody Car car) throws Exception {
        car.setStatmentId("Car.insert");
        String status = "failure";
        carService.insert(car);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Car
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Car car) throws Exception {
        car.setStatmentId("Car.update");
        String status = "failure";
        carService.update(car);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Car
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Car car) throws Exception {
        car.setStatmentId("Car.delete");
        String status = "failure";
        carService.batchDelete(car);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Car
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Car car = new Car();
        car.setStatmentId("Car.queryAsList");
        BeanUtils.populate(car, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(car, pageMap);
        List<BaseModel> carList = carService.queryAsList(car);
        Map<String, Object> renderMap = GridUIMapper.render(car, carList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of Car
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Car car) throws Exception {
        car.setStatmentId("Car.queryAsObject");
        return carService.queryAsObject(car);
    }

    @RequestMapping("/page")
    public String forword() {
        return "car/carList";
    }
    
    @RequestMapping("/queryCarAttach")
    @ResponseBody
    public Object queryMailAttach(Car car) throws Exception {
        AttachLink attachlink = new AttachLink();
        attachlink.setEntityid(car.getCarid());
        return attachmentService.querybyEntityid(attachlink);
    }


    @Resource
    public void setCarService(CarService carService) {
        this.carService = carService;
    }
    
    @Resource
    public void setAttachmentService(AttachmentService attachmentService) {
        this.attachmentService = attachmentService;
    }
}
